package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.contractmanage.querybean.ContractManageQueryBean;
import com.koron.css2.contractmanage.vo.ContractManageVO;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.vo.PjMeterContractExportVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class PjMeterContractExport extends AbstractExportExcel {

    private static Logger logger = LoggerFactory.getLogger(PjMeterContractExport.class);

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {


            ContractManageVO queryBean = JsonUtils.objectToPojo(req.getData(), ContractManageVO.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            BankMapper bankMapper = factory.getMapper(BankMapper.class);
            List<PjMeterContractExportVO> resultList = new ArrayList<>();
            List<PjMeter> meterList = pjMeterMapper.getListByConsultId(queryBean.getProjectId());

            Objects.requireNonNull(meterList, "参数错误");
            ContractManageMapper contractManageMapper = factory.getMapper(ContractManageMapper.class);
            Map<String, String> cerTypeMap = Tools.mapDicByCode(factory, "IDT");//证件类型
            Map<String, String> contractStatusMap = Tools.mapDicByCode(factory, "CONTRACT_STATUS");
            for (PjMeter pjMeter : meterList) {
                PjMeterContractExportVO pjMeterContractExportVO = new PjMeterContractExportVO();
                UserBean userBean = userInfoMapper.getUserInfoById(pjMeter.getUserInfoTempId(), UserBean.USERTEMP);
                AccountInfoBean accountInfo = accountInfoMapper.getAccountInfoById(pjMeter.getUserAccountTempId(), AccountInfoBean.ACCOUNTTEMP);
                // 开户银行名称
                if (StringUtils.isNotEmpty(accountInfo.getOpenAccountBank())){
                    BankBean bank = bankMapper.findBankById(accountInfo.getOpenAccountBank());
                    pjMeterContractExportVO.setOpenAccountBankName(bank.getBankName());
                }
                pjMeterContractExportVO.setUserNo(userBean.getUserNo());
                pjMeterContractExportVO.setCtmName(userBean.getCtmName());
                if (userBean != null && StringUtils.isNotBlank(userBean.getCertType())) {
                    pjMeterContractExportVO.setCertTypeName(cerTypeMap.get(userBean.getCertType()));
                }
                pjMeterContractExportVO.setCertNo(userBean.getCertNo());
                pjMeterContractExportVO.setCtmAddr(userBean.getCtmAddr());
                pjMeterContractExportVO.setMobile(userBean.getMobile());
                pjMeterContractExportVO.setStopWaterLinkMobile(userBean.getStopWaterLinkMobile());
                pjMeterContractExportVO.setUseWaterTypeName(userBean.getUseWaterTypeName());
                pjMeterContractExportVO.setSetupMeterDate(userBean.getSetupMeterDate());
                // 开户名
                pjMeterContractExportVO.setOpenAccountName(accountInfo.getOpenAccountName());
                // 银行账号
                pjMeterContractExportVO.setBankAccount(accountInfo.getBankAccount());
                //供水合同
                ContractManageQueryBean contractManageQueryBean = new ContractManageQueryBean();
                contractManageQueryBean.setUserNo(userBean.getUserNo());
                contractManageQueryBean.setContractTypes(new String[]{ContractTypeEnum.CONTRACT_TYPE_2.getContractType(), ContractTypeEnum.CONTRACT_TYPE_3.getContractType()});
                ContractManageVO gscontractManageVO = contractManageMapper.getContractManage(contractManageQueryBean);
                if (ObjectUtil.isNotNull(gscontractManageVO)&&StringUtils.isNotBlank(gscontractManageVO.getContractNo())) {
                    // 将合同状态转为中文
                    String contractStatusName = contractStatusMap.get(gscontractManageVO.getContractStatus());
                    pjMeterContractExportVO.setContractStatus(contractStatusName);
                    pjMeterContractExportVO.setContractNo(gscontractManageVO.getContractNo());
                    pjMeterContractExportVO.setContractName(gscontractManageVO.getContractName());
                }else {
                    // 默认为未签署状态
                    pjMeterContractExportVO.setContractStatus("未签署");
                }


                resultList.add(pjMeterContractExportVO);


            }


            // 4. 导出excel
            AttachmentDto attachment = new AttachmentDto();
            attachment.setBtlTemplate("PjMeterContractExport.btl");
            attachment.setData(resultList);
            attachment.setFilename("合同管理水表导出模板");
            attachment.setFileTemplate("PjMeterContractExport.xlsx");
            attachment.setFileType("excel");
            return attachment;
        } catch (Exception e) {
            logger.error("导出失败", e);
            throw new RuntimeException("获取导出数据失败");

        }
    }
}
